package org.example;

import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTPPr;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTString;

import java.io.FileInputStream;
import java.io.IOException;

public class ReadDocxTableTitle {

    public static void main(String[] args) {
        try {
            FileInputStream fis = new FileInputStream("/home/stark/Documents/table-demo.docx");
            XWPFDocument document = new XWPFDocument(fis);

            for (XWPFParagraph paragraph : document.getParagraphs()) {
                CTPPr ppr = paragraph.getCTP().isSetPPr() ? paragraph.getCTP().getPPr() : null;
                CTString style = ppr != null && ppr.isSetPStyle() ? ppr.getPStyle() : null;

                System.out.println(style != null);
                if (style != null) {
                    System.out.println(style.getVal());
                }
                if (style != null && "Heading1".equals(style.getVal())) { // 假设使用Heading1样式作为标题
                    String title = paragraph.getText();
                    System.out.println("Table Title: " + title);
                }
            }

            fis.close();
        } catch (IOException e) {
            e.fillInStackTrace();
        }
    }
}
